package com.angel.email.configuration.builders;

import com.angel.common.keyValue.KeyValueResult;
import com.angel.common.keyValue.impl.KeyValueResultImpl;
import com.angel.email.configuration.EmailConfiguration;
import com.angel.email.constants.EmailPropertyConstants;
import com.angel.email.exceptions.EmailAccessException;

/**
 *
 * @author William
 *
 */
public class EmailConfigurationBuilder {

	private Boolean debugMode = Boolean.TRUE;
	private Boolean authorize = Boolean.TRUE;
	private OutgoingEmailConfigurationBuilder outgoingConfigurationBuilder;
	private IncomingEmailConfigurationBuilder incomingConfigurationBuilder;

	public EmailConfigurationBuilder(
			OutgoingEmailConfigurationBuilder outgoingConfigurationBuilder,
			IncomingEmailConfigurationBuilder incomingConfigurationBuilder){
		this();
		this.setOutgoingConfigurationBuilder(outgoingConfigurationBuilder);
		this.setIncomingConfigurationBuilder(incomingConfigurationBuilder);
	}

	public EmailConfigurationBuilder(){
		super();
	}

	public EmailConfigurationBuilder(Boolean debugMode){
		super();
		this.setDebugMode(debugMode);
	}

	public EmailConfiguration buildEmailConfiguration() {
		this.checkEmailConfigurationState();

		EmailConfiguration ec = new EmailConfiguration();
		KeyValueResult defaultProperties = this.buildDefaultEmailConfiguration();
		ec.addKeyValueResult(defaultProperties);

		if(this.getOutgoingConfigurationBuilder() != null){
			KeyValueResult outgoingProperties = this.getOutgoingConfigurationBuilder().buildOutgoingEmailConfiguration();
			ec.setOutgoingAccountInformation(this.getOutgoingConfigurationBuilder().getAccountInformation());
			ec.addKeyValueResult(outgoingProperties);
		}
		if(this.getIncomingConfigurationBuilder() != null){
			KeyValueResult incomingProperties = this.getIncomingConfigurationBuilder().buildIncomingEmailConfiguration();
			ec.setIncomingAccountInformation(this.getIncomingConfigurationBuilder().getAccountInformation());
			ec.addKeyValueResult(incomingProperties);
		}
		return ec;
	}

	protected void checkEmailConfigurationState() {
		if(this.getIncomingConfigurationBuilder() == null && this.getOutgoingConfigurationBuilder() == null){
			throw new EmailAccessException("Cannot create an incoming/outgoing email configuration because they weren't setted.");
		} else {
			if(this.getIncomingConfigurationBuilder() != null && !this.getIncomingConfigurationBuilder().hasAccountInformation()){
				throw new EmailAccessException("Cannot create an incoming email configuration because it needs an account information.");
			}
			if(this.getOutgoingConfigurationBuilder() != null && !this.getOutgoingConfigurationBuilder().hasAccountInformation()){
				throw new EmailAccessException("Cannot create an outgoing email configuration because it needs an account information.");
			}
		}
	}

	private KeyValueResult buildDefaultEmailConfiguration() {
		KeyValueResult defaultProperties = new KeyValueResultImpl();
		defaultProperties.addKeyValue(EmailPropertyConstants.EMAIL_DEBUG, this.getDebugMode().toString());
		return defaultProperties;
	}

	/**
	 * @return the debugMode
	 */
	public Boolean getDebugMode() {
		return debugMode;
	}

	/**
	 * @param debugMode the debugMode to set
	 */
	public void setDebugMode(Boolean debugMode) {
		this.debugMode = debugMode;
	}

	/**
	 * @return the authorize
	 */
	public Boolean getAuthorize() {
		return authorize;
	}

	/**
	 * @param authorize the authorize to set
	 */
	public void setAuthorize(Boolean authorize) {
		this.authorize = authorize;
	}

	/**
	 * @return the outgoingConfigurationBuilder
	 */
	public OutgoingEmailConfigurationBuilder getOutgoingConfigurationBuilder() {
		return outgoingConfigurationBuilder;
	}

	/**
	 * @param outgoingConfigurationBuilder the outgoingConfigurationBuilder to set
	 */
	public void setOutgoingConfigurationBuilder(
			OutgoingEmailConfigurationBuilder outgoingConfigurationBuilder) {
		this.outgoingConfigurationBuilder = outgoingConfigurationBuilder;
	}

	/**
	 * @return the incomingConfigurationBuilder
	 */
	public IncomingEmailConfigurationBuilder getIncomingConfigurationBuilder() {
		return incomingConfigurationBuilder;
	}

	/**
	 * @param incomingConfigurationBuilder the incomingConfigurationBuilder to set
	 */
	public void setIncomingConfigurationBuilder(
			IncomingEmailConfigurationBuilder incomingConfigurationBuilder) {
		this.incomingConfigurationBuilder = incomingConfigurationBuilder;
	}
}
